<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Title</title>
  <style>
    html * {
      margin: 0;
    }

    #div1 {
      width: 100px;
      height: 100px;
      background: red;
      position: absolute;
      margin-left: 20px;
    }

  </style>
</head>
<body>
<div id="div1"></div>
<script>
  window.onload = function () {
    let d1 = new Dray("div1");
    d1.init();
    // oDiv.onmousedown = function (ev) {
    //   disX = ev.clientX - this.offsetLeft;
    //   disY = ev.clientY - this.offsetTop;
    //   document.onmousemove = function (ev) {
    //     oDiv.style.left = ev.clientX - disX + "px";
    //     oDiv.style.top = ev.clientY - disY + "px";
    //   };
    //   document.onmouseup = function () {
    //     document.onmousemove = null;
    //     document.onmouseup = null;
    //   }
    // }
  };

  function Dray(obj) {
    this.obj = document.getElementById(obj);
    this.disX = 0;
    this.disY = 0;
  }

  Dray.prototype.init = function () {
    let This = this;
    this.obj.onmousedown = function (e) {
      let ev = e || window.e;
      This.fnDown(ev);
      document.onmousemove = function (e) {
        let ev = e || window.e;
        This.fnMove(ev);
      };
      document.onmouseup = function () {
        This.fnUp();
      };
      return false;
    }
  };

  Dray.prototype.fnDown = function (ev) {
    this.disX = ev.clientX - this.obj.offsetLeft;
    this.disY = ev.clientY - this.obj.offsetTop;
  };

  Dray.prototype.fnMove = function (ev) {
    this.obj.style.left = ev.clientX - this.disX + "px";
    this.obj.style.top = ev.clientY - this.disY + "px";
  };

  Dray.prototype.fnUp = function () {
    document.onmousemove = null;
    document.onmouseup = null;
  }

</script>
</body>
</html>
